package com.oa.controller;

import com.oa.vo.Message;
import com.oa.vo.Page;
import com.oa.vo.Result;
import com.oa.pojo.Employee;
import com.oa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@CrossOrigin
@ResponseBody
@Controller
public class EmployeeConntroller {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/getEmployeePage")
    public Result getEmployeePage(Integer pageNo, Integer pageSize){
        if(pageNo == null){
            pageNo=1;
        }
        if(pageSize == null){
            pageSize=3;
        }
        Page page = employeeService.getEmployeePage(pageNo,pageSize);
        Message message = new Message();
        Result result = new Result();
        result.setData(page);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getEmployeeList")
    public Result getEmployeeList(){
        List<Employee> employeeList = employeeService.getEmployeeList();
        Message message = new Message();
        Result result = new Result();
        result.setData(employeeList);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getEmployee")
    public Result getEmployee(Integer empId){
        Employee employee = employeeService.getEmployee(empId);
        Message message = new Message();
        Result result = new Result();
        result.setData(employee);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/addEmployee")
    public Result addEmployee(@RequestBody Employee employee){
        employee.setEmpPassword("000000");
        employee.setRoleId(2);
        System.out.println(employee);
        Date date = new Date();
        employee.setHireTime(date);
        employeeService.addEmployeeMapper(employee);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/delEmployee")
    public Result delEmployee(Integer EmployeeId){
        Message message = new Message();
        Result result = new Result();
        if(EmployeeId == null){
            message.setCode(200);
            message.setMsg("No ID");
            result.setMessage(message);
            return result;
        }
        message.setCode(100);
        message.setMsg("ok");
        employeeService.delEmployee(EmployeeId);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/updateEmployee")
    public Result updateEmployee(@RequestBody Employee employee){
        Date date = new Date();
        employee.setHireTime(date);
        employeeService.updateEmployee(employee);
        Result result = new Result();
        return result;
    }

}
